package cn.yunhe.servlet;

import cn.yunhe.entity.Admin;
import cn.yunhe.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");

        Admin admin = new Admin(name, pwd);

        AdminService adminService = new AdminService();
        boolean flag = adminService.registerAdmin(admin);

        if (flag) {
            response.sendRedirect("login.html");
        } else {
            response.sendRedirect("register.html");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
